/**
 * 代码生成API
 */

import type {
  CodeGenerationHistoryQuery,
  CodeGenerationRequest,
  EnhancedCodeGenerationRequest,
  ProjectGenerationResponse,
  CodePreviewResponse,
  FrameworkType
} from "./type"
import { http } from "@/http/index"

/** 代码生成API */
export const codeGenerationApi = {
  /**
   * 验证生成配置
   * @param params 代码生成配置参数
   */
  validateConfig: (params: CodeGenerationRequest) => {
    return http.post<Record<string, any>>("/code-generation/validate", params)
  },

  /**
   * 预览生成代码
   * @param params 代码生成配置参数
   */
  previewCode: (params: CodeGenerationRequest) => {
    return http.post<Record<string, any>>("/code-generation/preview", params)
  },

  /**
   * 生成项目代码
   * @param params 代码生成配置参数
   */
  generateCode: (params: CodeGenerationRequest) => {
    return http.post<Record<string, any>>("/code-generation/generate", params)
  },

  /**
   * 获取模板配置
   * @param frameworkType 框架类型
   */
  getTemplateConfig: (frameworkType: FrameworkType) => {
    return http.get<Record<string, any>>(`/code-generation/template-config/${frameworkType}`)
  },

  /**
   * 获取生成历史
   * @param params 查询参数
   */
  getGenerationHistory: (params: CodeGenerationHistoryQuery) => {
    return http.get<Record<string, any>>("/code-generation/history", { params })
  },

  /**
   * 获取支持的框架
   */
  getSupportedFrameworks: () => {
    return http.get<Record<string, any>>("/code-generation/frameworks")
  },

  /**
   * 下载生成的代码
   * @param generationId 生成记录ID
   */
  downloadGeneratedCode: (generationId: number) => {
    return http.get<Record<string, any>>(`/code-generation/download/${generationId}`)
  },

  /**
   * 生成完整项目
   * @param params 项目生成配置参数
   */
  generateProject: (params: CodeGenerationRequest) => {
    return http.post<ProjectGenerationResponse>("/code-generation/generate-project", params)
  },

  /**
   * 预览完整项目代码
   * @param params 项目生成配置参数
   */
  previewProject: (params: EnhancedCodeGenerationRequest) => {
    return http.post<CodePreviewResponse>("/code-generation/preview-project", params)
  },

  /**
   * 生成单表代码
   * @param tableId 表ID
   * @param params 生成配置参数
   */
  generateTableCode: (tableId: number, params: EnhancedCodeGenerationRequest) => {
    return http.post<CodePreviewResponse>(`/code-generation/generate-table/${tableId}`, params)
  },

  /**
   * 清理临时文件
   * @param generationId 生成记录ID
   */
  cleanupTempFiles: (generationId: string) => {
    return http.delete<{ success: boolean; message?: string }>(`/code-generation/cleanup/${generationId}`)
  }
}

export * from "./type"
